package com.yihr.ext.hv;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.regex.Pattern;

public class PasswordValidator implements ConstraintValidator<Password, String> {

    private static Pattern patternNumber = Pattern.compile("\\d+");
    private static Pattern patternLetter = Pattern.compile("[A-Za-z]+");
    private static Pattern patternALL = Pattern.compile("^[-0-9A-Za-z_+=*#$%!?|&*]{6,32}$");



    @Override
    public void initialize(Password constraintAnnotation) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (null == s || s.isEmpty()) {
            return false;
        }

        if (!patternNumber.matcher(s).find()) {
            return false;
        }
        if (!patternLetter.matcher(s).find()) {
            return false;
        }
        if (!patternALL.matcher(s).matches()) {
            return false;
        }
        return true;
    }
}
